package com.zhongjian.technologycal.mall.activity.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.adapter.KeyWordAdaper;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.KeyWordBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/11/17.
 * 通过关键字搜索结果列表
 */

public class KeywordSearchListActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.seach_key_listview)
    ListView seach_key_listview;
    private String keyword;
    private List<KeyWordBean> listItems = new ArrayList<>();
    private KeyWordAdaper keyWordAdaper;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    keyWordAdaper.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        keyword = getIntent().getStringExtra("keywordTags");
        tv_main_title.setText(keyword);
        initRequestMembersArea();

        keyWordAdaper = new KeyWordAdaper(getBaseContext(),listItems);
        seach_key_listview.setAdapter(keyWordAdaper);
        seach_key_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intentOther = new Intent();
                intentOther.setClass(getBaseContext(), ProductsDetailsActivity.class);
                intentOther.putExtra("productId", String.valueOf(listItems.get(i).getProduct().getId()));
                startActivity(intentOther);
            }
        });


    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_keyword_search_list;
    }

    /**
     * 通过关键字搜索商品数据
     */
    private void initRequestMembersArea() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else {
            HttpRetrofitClient.getInstance(getBaseContext())
                    .postUrlRequestUpdateUserName(keyword,
                            new Subscriber<ResponseBody>() {
                                @Override
                                public void onStart() {
                                    super.onStart();

                                }

                                @Override
                                public void onCompleted() {


                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(ResponseBody responseBody) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(responseBody.string());
                                        JSONArray jsonRecommend = jsonObject.optJSONArray("data");
                                        Gson gson = new Gson();
                                        List<KeyWordBean> forList = new ArrayList<>();
                                        for(int i = 0; i < jsonRecommend.length(); i++){
                                            KeyWordBean membersAreasBean = gson.fromJson(jsonRecommend.optString(i).toString(),KeyWordBean.class);
                                            forList.add(membersAreasBean);

                                        }
                                        setData(forList);

                                    } catch (Exception e) {
                                        e.printStackTrace();

                                    }

                                }
                            });
        }

    }

    private void setData(List<KeyWordBean> foreshowList) {
        for (KeyWordBean vo : foreshowList) {
            listItems.add(vo);
        }
        handler.sendEmptyMessage(0);

    }
    @OnClick(R.id.iv_back_page)
    public void onClickBack(){
        finish();
    }

    @Override
    public void initPresenter() {

    }
}
